
#include "page_fetcher.h"

PageFetcher::PageFetcher()
{
  curl_ = curl_easy_init();
  curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, PageFetcher::Callback_);
}

PageFetcher::~PageFetcher()
{
  curl_easy_cleanup(curl_);
}

bool PageFetcher::Fetch(const string &url, string *page_content)
{
  if(curl_ == NULL) return false;
  curl_easy_setopt(curl_, CURLOPT_URL, url.c_str());
  curl_easy_setopt(curl_, CURLOPT_WRITEDATA, page_content);
  CURLcode code = curl_easy_perform(curl_);
  return code == CURLE_OK;
}

size_t PageFetcher::Callback_(void *buff,
                              size_t size,
                              size_t nmemb,
                              void *out)
{
  *(string*)out = (char*)buff;
}
